package cn.chenli.job.common.controller;

import cn.chenli.job.config.GlobalConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author chenli
 * @create 2017-08-11
 * urlPatterns = {"*.do"}
 */

//@WebServlet(name = "test", urlPatterns = {"/"})
@WebServlet(
        urlPatterns = {"/"}, // 需要定义Freemarker解析的页面后缀类型
        loadOnStartup = 0,
        name = "MyServlet",
        displayName = "MyServlet",
        initParams = {
                @WebInitParam(name = "NoCache", value = "true"),//定义是否缓存
                @WebInitParam(name = "ContentType", value = "text/html; charset=UTF-8"),// 定义内容类型
                @WebInitParam(name = "template_update_delay", value = "0"), // 开发环境中可设置为0
                @WebInitParam(name = "default_encoding", value = "UTF-8"),
                @WebInitParam(name = "number_format", value = "0.##########")
        })
public class MyServlet extends HttpServlet {

    private static final Logger log = LoggerFactory.getLogger(MyServlet.class);

    @Override
    public void init() throws ServletException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doRequest(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doRequest(req, resp);
    }

    private void doRequest(HttpServletRequest req, HttpServletResponse resp) {
        String servletPath = req.getServletPath();
        if("/".equals(servletPath)){
            servletPath = "/index";
        }
        if(servletPath.contains(".")){
            servletPath = servletPath.substring(0,servletPath.indexOf("."));
        }
        Action action = GlobalConfig.controllerMap.get(servletPath);
        if(action == null){
            throw new RuntimeException("没有该请求:"+servletPath);
        }
        Class<? extends Controller> controllerClazz = action.getController();

        if(controllerClazz == null){
            throw new RuntimeException("没有该请求"+servletPath);
        }
        Controller controller;
        try {
            controller = controllerClazz.newInstance();
            req.setCharacterEncoding("utf-8");
            controller.setParams(req,resp,action.getViewPath());
            Method method = controllerClazz.getMethod(action.getMethod());
            method.invoke(controller);
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | UnsupportedEncodingException | InvocationTargetException  e) {
            log.info("请求失败："+servletPath);
            e.printStackTrace();
        }
        log.info("请求成功："+servletPath);
    }

}